package com.time.workshop;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.time.workshop.bean.BaseResponse;
import com.time.workshop.bean.OrderType;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.bean.ResponseOrder;
import com.time.workshop.bean.UserInfo;
import com.time.workshop.ui.LoginActivity;
import com.time.workshop.ui.OrderListActivity;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.Log;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;

public class API
{

	/** 测试网络路径 */
//	public static final String URL = "http://121.40.48.98:9090/tm";// 端口 9090
																	// 8080
	/** 正式网络路径 */
	 public static final String URL = "http://api.timeworkshop.cn/tm";

	/** 注册 */
	public static final String reg = URL + "/regeister";
	/** 短信发送 */
	public static final String sms = URL + "/send_sms";
	/** 登录 */
	public static final String login = URL + "/login";
	/** 修改密码 */
	public static final String modfiyPwd = URL + "/edit_password";
	/** 忘记密码 */
	public static final String forgetPwd = URL + "/forget_password";
	/** 首页头部图片 */
	public static final String headImage = URL + "/api/advert_list";
	/** 地区列表 */
	public static final String areaList = URL + "/api/area_list";
	/** 店铺列表 */
	public static final String shopList = URL + "/api/store_list";
	/** 订单列表 */
	public static final String orderList = URL + "/api/order_list";
	/** 订单总数 */
	public static final String orderCount = URL + "/api/order_count";
	/** 提交订单 */
	public static final String submitOrder = URL + "/api/submit_order";
	/** 订单详情 */
	public static final String orderDetail = URL + "/api/order_info";
	/** 店铺医院详情 */
	public static final String shopDetail = URL + "/api/store_info";
	/** 支付接口 */
	public static final String pay = URL + "/api/pay";
	/** 意见反馈 */
	public static final String feedback = URL + "/feedback";
	/** 科室列表 */
	public static final String departmentList = URL + "/api/department_list";
	/** 专家列表 */
	public static final String personList = URL + "/api/person_list";
	/** 选择车型 */
	public static final String car_type = URL + "/api/car_type";
	/** 上传头像 */
	public static final String user_face_img = URL + "/header_image";
	/** 发布需求 */
	public static final String add_task = URL + "/api/need/add";
	/** 获取需求信息 */
	public static final String task_detail = URL + "/api/need/info";
	/** 需求大厅列表 */
	public static final String task_list = URL + "/api/need/need_list";
	/** 揭榜 */
	public static final String task_accept = URL + "/api/need/accept";
	/** 用户个人需求列表 */
	public static final String task_user_need = URL + "/api/need/user_need";
	/** 用户揭榜列表 */
	public static final String task_user_accept = URL + "/api/need/user_accept";
	/** 榜单支付接口 */
	public static final String need_pay = URL + "/api/need_pay";

	/** 返回数据总体的实体类 */
	private class Response extends BaseResponse<UserInfo>
	{
	};

	private static Response mUserInfo;

	private static RequestOrder requestOrder;

	/**
	 * 提交订单
	 * 
	 * @param orderBean
	 * @param context
	 */
	public static void submitOrder(final RequestOrder orderBean, final SuperActivity mSuperActivity)
	{
		requestOrder = orderBean;
		if ("".equals(CommonUtils.isNull(PreferenceUtil.getString(mSuperActivity, BaseConstant.PREF_TOKE_STRING))) || "".equals(CommonUtils.isNull(PreferenceUtil.getString(mSuperActivity, BaseConstant.PREF_USERNAME_STRING))) || "".equals(CommonUtils.isNull(PreferenceUtil.getString(mSuperActivity, BaseConstant.PREF_PASSWORD_STRING))))
		{
			Intent in = new Intent(mSuperActivity, LoginActivity.class);
			mSuperActivity.startActivity(in, true);
			return;
		}
		if (!CommonUtils.isNetworkConnected(mSuperActivity))
		{
			NewToast.makeText(mSuperActivity, mSuperActivity.getResources().getString(R.string.M000));
			return;
		}
		RequestParams param = new RequestParams();
		param.addBodyParameter("ORDER_TYPE", orderBean.ORDER_TYPE);
		param.addBodyParameter("remark", orderBean.remark);
		param.addBodyParameter("contactor_person", orderBean.contactor_person);
		param.addBodyParameter("contactor_phone", orderBean.contactor_phone);
		param.addBodyParameter("contactor_address", orderBean.contactor_address);
		param.addBodyParameter("reservation_person", orderBean.reservation_person);
		param.addBodyParameter("reservation_time", orderBean.reservation_time);
		param.addBodyParameter("store_id", orderBean.store_id);
		param.addBodyParameter("ID_CARD", orderBean.ID_CARD);
		param.addBodyParameter("STORE_ADDRESS", orderBean.STORE_ADDRESS);
		param.addBodyParameter("DEPARTMENT_ID", orderBean.DEPARTMENT_ID);
		param.addBodyParameter("PERSON_ID", orderBean.PERSON_ID);
		param.addBodyParameter("AGE", orderBean.AGE);
		param.addBodyParameter("CAR_TYPE", orderBean.CAR_TYPE);
		param.addBodyParameter("SEND_ADDRESS", orderBean.SEND_ADDRESS);
		param.addBodyParameter("token", PreferenceUtil.getString(mSuperActivity, BaseConstant.PREF_TOKE_STRING));
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, API.submitOrder, param, new RequestCallBack<String>()
		{

			@Override
			public void onStart()
			{
				super.onStart();
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo)
			{
				Log.d("onSuccess==", responseInfo + "");
				mSuperActivity.dismissLoadingDialog();
				try
				{
					Gson gson = new Gson();
					ResponseOrder responseOrder = gson.fromJson(responseInfo.result, ResponseOrder.class);
					Log.d("responseOrder==", responseOrder.toString());
					if (responseOrder.success)
					{
						NewToast.makeText(mSuperActivity, responseOrder.message);
						Intent in = new Intent(mSuperActivity, OrderListActivity.class);
						in.putExtra("orderId", responseOrder.result.getID());
						in.putExtra("status", "process");
						mSuperActivity.startActivity(in, true);
						if (!orderBean.ORDER_TYPE.equals(OrderType.SJGF.toString()))
						{
							mSuperActivity.finish();
						}
					} else
					{
						NewToast.makeText(mSuperActivity, responseOrder.message);
						if (1 == responseOrder.resCode)
						{
							doLoginRequest(PreferenceUtil.getString(mSuperActivity, BaseConstant.PREF_USERNAME_STRING), PreferenceUtil.getString(mSuperActivity, BaseConstant.PREF_PASSWORD_STRING), mSuperActivity);
						}
					}
				} catch (Exception e)
				{
					e.printStackTrace();
					NewToast.makeText(mSuperActivity, e.getMessage());
				}
			}

			@Override
			public void onFailure(HttpException error, String msg)
			{
				Log.d("onFailure==", msg + "");
				NewToast.makeText(mSuperActivity, "fail" + msg);
				mSuperActivity.dismissLoadingDialog();
			}
		});
	}

	/** token过期后再次登录 */
	private static void doLoginRequest(String pho, final String pwd, final SuperActivity mSuperActivity)
	{
		mSuperActivity.showDialog(null);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("username", pho);
		params.addQueryStringParameter("password", pwd);

		HttpUtils httpUtils = new HttpUtils();

		httpUtils.send(HttpMethod.GET, API.login, params, new RequestCallBack<String>()
		{

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo)
			{
				Log.d("onSuccess==", responseInfo.result + "");

				mSuperActivity.dismissLoadingDialog();
				try
				{
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					Gson gson = new Gson();

					mUserInfo = gson.fromJson(jsonObject.toString(), Response.class);

					Log.d("mUserInfo", mUserInfo.getResult() + "");

					if (mUserInfo.isSuccess())
					{

						// 保存登录返回的Token
						PreferenceUtil.putString(mSuperActivity, BaseConstant.PREF_TOKE_STRING, mUserInfo.getResult().getTOKEN() + "");
						submitOrder(requestOrder, mSuperActivity);
					} else
					{
						NewToast.makeText(mSuperActivity, mUserInfo.getMessage());
					}
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg)
			{
				mSuperActivity.dismissLoadingDialog();
				Log.d("onFailure==", msg + "");
				NewToast.makeText(mSuperActivity, R.string.login_failure);
			}

		});
	}
}
